﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Shsc.Net.Common.Util
{
    /// <summary>
    /// Check 工具
    /// </summary>
    public static class CheckUtil
    {

        /// <summary>
        /// 必要チェック
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool CheckReqired(object value)
        {
            if (value == null)
            {
                return false;
            }

            if (value is String)
            {
                if (Convert.ToString(value).Trim() == string.Empty)
                {
                    return false;
                }
            }
            else if (value is ICollection)
            {
                if (((ICollection)value).Count == 0)
                {
                    return false;
                }
            }
            else if (value.GetType().IsArray)
            {
                //TODO
                return true;
            }
            
            return true;
        }

        /// <summary>
        /// 日期Check
        /// </summary>
        /// <param name="value"></param>
        /// <param name="pattarn"></param>
        /// <returns></returns>
        public static bool CheckDateByPattarn(object value, string pattarn)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            if (!(value is string)) return true;

            try
            {
                DateTime tmpDate = DateTime.ParseExact((value as string), pattarn, null);
                string convertDate = tmpDate.ToString(pattarn);
                if (convertDate != (value as string))
                {
                    return false;
                }
            }
            catch (FormatException)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 根据正则验证
        /// </summary>
        /// <param name="value"></param>
        /// <param name="regexPattarn"></param>
        /// <returns></returns>
        public static bool CheckByRegex(object value, string regexPattarn)
        {
            if (!CheckReqired(value))
            {
                return true;
            }
            string strvalue = Convert.ToString(value);
            Regex rx = new Regex(regexPattarn);
            return rx.IsMatch(strvalue);
        }

        /// <summary>
        /// 数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool CheckIsNumiric(object value)
        {
            return CheckByRegex(value, @"^-?\d+(.\d+){0,1}$");
        }

        /// <summary>
        /// 整数Check
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool CheckInt(object value)
        {
            return CheckByRegex(value, @"^0|-?[1-9]+\d*$");
        }

        /// <summary>
        /// FlaotCheck
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool CheckFloat(object value)
        {
            return CheckByRegex(value, @"^0|-?[1-9]+\d*(.\d*[1-9]{1})$");
        }

        /// <summary>
        /// 整数范围Check
        /// </summary>
        /// <param name="value"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static bool CheckRange(object value, long min, long max)
        {
            if (value == null)
            {
                return true;
            }

            if (CheckIsNumiric(value))
            {
                long tmpNum = Convert.ToInt64(value);
                return min <= tmpNum && max >= tmpNum;
            }
            return false;
        }

        /// <summary>
        /// 最大長度チェック
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool CheckMaxLength(object value, int maxLength)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            return !(Convert.ToString(value).Length > maxLength);
        }

        /// <summary>
        /// 最大長度（バイト）チェック
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool CheckMaxLengthByByte(object value, int maxLength)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            return !(Encoding.Default.GetBytes(Convert.ToString(value)).Length > maxLength);
        }

        /// <summary>
        /// 最小長度チェック
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool CheckMinLength(object value, int minLength)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            return !(Convert.ToString(value).Length < minLength);
        }

        /// <summary>
        /// 最小長度（バイト）チェック
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool CheckMinLengthByByte(object value, int minLength)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            return !(Encoding.Default.GetBytes(Convert.ToString(value)).Length < minLength);
        }

        /// <summary>
        /// 同
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool CheckFixLength(object value, int length)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            return !(Convert.ToString(value).Length == length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool CheckFixByteLength(object value, int length)
        {
            if (!CheckReqired(value))
            {
                return true;
            }

            return !(Encoding.Default.GetBytes(Convert.ToString(value)).Length == length);
        }
    }
}
